package com.cskaoyan.service.pz;

import com.cskaoyan.bean.MallSystem;
import com.cskaoyan.bean.pz.WxSystem;

import com.cskaoyan.mapper.pz.ConfigMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
class ConfigServiceImpl implements ConfigService {

    @Autowired
    ConfigMapper configMapper;

    @Override
    public MallSystem queryMallSystem() {
        MallSystem mallSystem = new MallSystem();
        mallSystem.setCskaoyanmall_mall_name(selectKeyValue("cskaoyan_mall_mall_name"));
        mallSystem.setCskaoyanmall_mall_qq(selectKeyValue("cskaoyan_mall_mall_qq"));
        mallSystem.setCskaoyanmall_mall_phone(selectKeyValue("cskaoyan_mall_mall_phone"));
        mallSystem.setCskaoyanmall_mall_address(selectKeyValue("cskaoyan_mall_mall_address"));
        return mallSystem;
    }

    @Override
    public void updateMallSystem(MallSystem mallSystem) {
        updateKeyValue("cskaoyan_mall_mall_name",mallSystem.getCskaoyanmall_mall_name());
        updateKeyValue("cskaoyan_mall_mall_qq",mallSystem.getCskaoyanmall_mall_qq());
        updateKeyValue("cskaoyan_mall_mall_phone",mallSystem.getCskaoyanmall_mall_phone());
        updateKeyValue("cskaoyan_mall_mall_address",mallSystem.getCskaoyanmall_mall_address());
    }

    @Override
    public Map<String, String> queryExpress() {
        Map<String, String> map = new HashMap<>();
        map.put("cskaoyanmall_express_freight_value",selectKeyValue("cskaoyan_mall_express_freight_value"));
        map.put("cskaoyanmall_express_freight_min",selectKeyValue("cskaoyan_mall_express_freight_min"));
        return map;
    }

    @Override
    public void updateExpress(Map<String, String> map) {
        updateKeyValue("cskaoyan_mall_express_freight_value",map.get("cskaoyanmall_express_freight_value"));
        updateKeyValue("cskaoyan_mall_express_freight_min",map.get("cskaoyanmall_express_freight_min"));
    }

    @Override
    public Map<String, String> queryOrder() {
        Map<String, String> map = new HashMap<>();
        map.put("cskaoyanmall_order_unconfirm",selectKeyValue("cskaoyan_mall_order_unconfirm"));
        map.put("cskaoyanmall_order_unpaid",selectKeyValue("cskaoyan_mall_order_unpaid"));
        map.put("cskaoyanmall_order_comment",selectKeyValue("cskaoyan_mall_order_comment"));
        return map;
    }

    @Override
    public void updateOrder(Map<String, String> map) {
        updateKeyValue("cskaoyan_mall_order_unconfirm",map.get("cskaoyanmall_order_unconfirm"));
        updateKeyValue("cskaoyan_mall_order_unpaid",map.get("cskaoyanmall_order_unpaid"));
        updateKeyValue("cskaoyan_mall_order_comment",map.get("cskaoyanmall_order_comment"));
    }

    @Override
    public WxSystem queryWx() {
        WxSystem wxSystem = new WxSystem(
                selectKeyValue("cskaoyan_mall_wx_share"),
                selectKeyValue("cskaoyan_mall_wx_index_brand"),
                selectKeyValue("cskaoyan_mall_wx_index_topic"),
                selectKeyValue("cskaoyan_mall_wx_index_hot"),
                selectKeyValue("cskaoyan_mall_wx_catlog_goods"),
                selectKeyValue("cskaoyan_mall_wx_catlog_list"),
                selectKeyValue("cskaoyan_mall_wx_index_new")
        );
        return wxSystem;
    }

    @Override
    public void updateWx(WxSystem wxSystem) {
        updateKeyValue("cskaoyan_mall_wx_share",wxSystem.getCskaoyanmall_wx_share());
        updateKeyValue("cskaoyan_mall_wx_index_brand",wxSystem.getCskaoyanmall_wx_index_brand());
        updateKeyValue("cskaoyan_mall_wx_index_topic",wxSystem.getCskaoyanmall_wx_index_topic());
        updateKeyValue("cskaoyan_mall_wx_index_hot",wxSystem.getCskaoyanmall_wx_index_hot());
        updateKeyValue("cskaoyan_mall_wx_catlog_goods",wxSystem.getCskaoyanmall_wx_catlog_goods());
        updateKeyValue("cskaoyan_mall_wx_catlog_list",wxSystem.getCskaoyanmall_wx_catlog_list());
        updateKeyValue("cskaoyan_mall_wx_index_new",wxSystem.getCskaoyanmall_wx_index_new());
    }

    /**
     * 根据表中key-name 的值修改key-value 的值
     */
    private void updateKeyValue(String keyName,String keyValue){
        configMapper.UpdateKeyName(keyName,keyValue);
    }

    /**
     * 根据表中key-name 的值查询key-value 的值
     */
    private String selectKeyValue(String keyName){
        String keyValue = configMapper.selectKeyName(keyName);
        return keyValue;
    }
}
